<template>
    <el-card class="box">
        <div style="height: 20px;">
            <span>个人信息</span>
        </div>

        <el-form label-width="80px" style="margin-top: 15px;">
            <el-upload style="text-align: center;" class="avatar-uploader" action="http://localhost:9090/file/upload"
                :show-file-list="false" :on-success="handleAvatarSuccess">
                <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <el-form-item label="用户名" width="50px">
                <el-input v-model="form.username" disabled autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
                <el-input v-model="form.nickname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="性别">
                <template>
                    <el-radio v-model="form.sex" label="男">男</el-radio>
                    <el-radio v-model="form.sex" label="女">女</el-radio>
                </template>
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model="form.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系电话">
                <el-input v-model="form.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="住址">
                <el-input v-model="form.address" type="textarea" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="position: relative; left: 70px;" @click="save">保 存</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
    export default {
        name: "Person",
        data() {
            return {
                form: {},
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
            }
        },
        created() {
            this.getUser().then(res => {
                console.log(res)
                this.form = res
            }) 
        },
        methods: {
            async getUser() {
                return (await this.request.get("/user/username/" + this.user.username)).data
            },
            // 保存
            save() {
                this.request.post("/user", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功！")
                        // 触发父级更新User的方法
                        this.$emit("refreshUser")
                        // 更新浏览器存储信息的用户信息
                        this.getUser().then(res => {
                            res.token = JSON.parse(localStorage.getItem("user")).token
                            localStorage.setItem("user", JSON.stringify(res))
                        })
                    } else {
                        this.$message.error("保存失败！")
                    }
                })
            },
            handleAvatarSuccess(res) {
                this.form.avatarUrl = res
            },
        }
    }
</script>

<style scoped>
    .box {
        width: 400px;
        margin: 0 auto;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 130px;
        height: 130px;
        display: block;
        margin-bottom: 10px;
        border-radius: 50%;
    }
</style>